#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
多场景测试结果分析脚本
分析所有测试场景的商品数据，提供详细的统计报告
"""

import json
import os
from collections import Counter
import re

def load_scenario_data():
    """加载所有场景的数据"""
    scenario_files = [
        'products_酒店用方巾纸__.json',
        'products_办公用品_广东_.json', 
        'products_餐具_浙江_杭州.json',
        'products_服装_江苏_.json',
        'products_电子产品__.json'
    ]
    
    scenarios = {}
    for file in scenario_files:
        if os.path.exists(file):
            with open(file, 'r', encoding='utf-8') as f:
                data = json.load(f)
                # 从文件名提取场景信息
                parts = file.replace('products_', '').replace('.json', '').split('_')
                keyword = parts[0]
                province = parts[1] if len(parts) > 1 and parts[1] else '全国'
                city = parts[2] if len(parts) > 2 and parts[2] else ''
                
                scenarios[keyword] = {
                    'keyword': keyword,
                    'province': province, 
                    'city': city,
                    'data': data
                }
    
    return scenarios

def analyze_price_distribution(prices):
    """分析价格分布"""
    if not prices:
        return {}
    
    price_ranges = {
        '0-10元': 0,
        '10-50元': 0, 
        '50-100元': 0,
        '100-500元': 0,
        '500元以上': 0
    }
    
    for price in prices:
        if price < 10:
            price_ranges['0-10元'] += 1
        elif price < 50:
            price_ranges['10-50元'] += 1
        elif price < 100:
            price_ranges['50-100元'] += 1
        elif price < 500:
            price_ranges['100-500元'] += 1
        else:
            price_ranges['500元以上'] += 1
    
    return price_ranges

def extract_keywords_from_titles(titles):
    """从标题中提取关键词"""
    all_words = []
    for title in titles:
        # 简单的中文分词（基于常见分隔符）
        words = re.findall(r'[\u4e00-\u9fff]+', title)
        for word in words:
            if len(word) >= 2:  # 只保留2个字符以上的词
                all_words.append(word)
    
    return Counter(all_words).most_common(10)

def analyze_scenario(scenario_name, scenario_data):
    """分析单个场景的数据"""
    data = scenario_data['data']
    
    # 基础统计
    total_products = len(data)
    
    # 提取数据
    titles = [item.get('title', '') for item in data if item.get('title')]
    prices = []
    sales = []
    links = [item.get('link', '') for item in data if item.get('link')]
    tags_list = []
    
    for item in data:
        # 价格处理
        price_str = item.get('price', '')
        if price_str and price_str != 'N/A':
            try:
                price = float(price_str)
                prices.append(price)
            except:
                pass
        
        # 销量处理
        sales_str = item.get('sales', '')
        if sales_str and sales_str != 'N/A':
            try:
                # 提取数字
                sales_num = re.findall(r'\d+', str(sales_str))
                if sales_num:
                    sales.append(int(sales_num[0]))
            except:
                pass
        
        # 标签处理
        tags = item.get('tags', [])
        if tags:
            tags_list.extend(tags)
    
    # 统计分析
    analysis = {
        'scenario_info': {
            'keyword': scenario_data['keyword'],
            'province': scenario_data['province'],
            'city': scenario_data['city']
        },
        'basic_stats': {
            'total_products': total_products,
            'has_title': len(titles),
            'has_price': len(prices),
            'has_sales': len(sales),
            'has_link': len([l for l in links if l]),
            'has_tags': len([item for item in data if item.get('tags')])
        },
        'price_analysis': {},
        'sales_analysis': {},
        'top_keywords': [],
        'top_tags': [],
        'sample_products': data[:3]  # 前3个商品作为样例
    }
    
    # 价格分析
    if prices:
        analysis['price_analysis'] = {
            'min_price': min(prices),
            'max_price': max(prices),
            'avg_price': round(sum(prices) / len(prices), 2),
            'median_price': sorted(prices)[len(prices)//2],
            'price_distribution': analyze_price_distribution(prices)
        }
    
    # 销量分析
    if sales:
        analysis['sales_analysis'] = {
            'min_sales': min(sales),
            'max_sales': max(sales),
            'avg_sales': round(sum(sales) / len(sales), 2),
            'median_sales': sorted(sales)[len(sales)//2]
        }
    
    # 关键词分析
    analysis['top_keywords'] = extract_keywords_from_titles(titles)
    
    # 标签分析
    if tags_list:
        analysis['top_tags'] = Counter(tags_list).most_common(10)
    
    return analysis

def generate_report(all_analysis):
    """生成综合报告"""
    print("\n" + "="*80)
    print("多场景1688商品爬取测试结果分析报告")
    print("="*80)
    
    # 总体概览
    total_products = sum(analysis['basic_stats']['total_products'] for analysis in all_analysis.values())
    total_scenarios = len(all_analysis)
    
    print(f"\n📊 总体概览:")
    print(f"  • 测试场景数: {total_scenarios}")
    print(f"  • 总商品数: {total_products}")
    print(f"  • 平均每场景商品数: {total_products // total_scenarios}")
    
    # 各场景详细分析
    for scenario_name, analysis in all_analysis.items():
        print(f"\n" + "-"*60)
        print(f"🔍 场景: {analysis['scenario_info']['keyword']}")
        if analysis['scenario_info']['province'] != '全国':
            location = analysis['scenario_info']['province']
            if analysis['scenario_info']['city']:
                location += f" {analysis['scenario_info']['city']}"
            print(f"📍 地区: {location}")
        
        stats = analysis['basic_stats']
        print(f"\n📈 数据完整性:")
        print(f"  • 总商品数: {stats['total_products']}")
        print(f"  • 标题完整率: {stats['has_title']}/{stats['total_products']} ({stats['has_title']/stats['total_products']*100:.1f}%)")
        print(f"  • 价格完整率: {stats['has_price']}/{stats['total_products']} ({stats['has_price']/stats['total_products']*100:.1f}%)")
        print(f"  • 销量完整率: {stats['has_sales']}/{stats['total_products']} ({stats['has_sales']/stats['total_products']*100:.1f}%)")
        print(f"  • 链接完整率: {stats['has_link']}/{stats['total_products']} ({stats['has_link']/stats['total_products']*100:.1f}%)")
        print(f"  • 标签完整率: {stats['has_tags']}/{stats['total_products']} ({stats['has_tags']/stats['total_products']*100:.1f}%)")
        
        # 价格分析
        if analysis['price_analysis']:
            price_info = analysis['price_analysis']
            print(f"\n💰 价格分析:")
            print(f"  • 价格区间: {price_info['min_price']}元 - {price_info['max_price']}元")
            print(f"  • 平均价格: {price_info['avg_price']}元")
            print(f"  • 中位数价格: {price_info['median_price']}元")
            
            print(f"  • 价格分布:")
            for range_name, count in price_info['price_distribution'].items():
                percentage = count / stats['has_price'] * 100 if stats['has_price'] > 0 else 0
                print(f"    - {range_name}: {count}个 ({percentage:.1f}%)")
        
        # 销量分析
        if analysis['sales_analysis']:
            sales_info = analysis['sales_analysis']
            print(f"\n📦 销量分析:")
            print(f"  • 销量区间: {sales_info['min_sales']} - {sales_info['max_sales']}")
            print(f"  • 平均销量: {sales_info['avg_sales']}")
            print(f"  • 中位数销量: {sales_info['median_sales']}")
        
        # 热门关键词
        if analysis['top_keywords']:
            print(f"\n🔥 热门关键词 TOP5:")
            for i, (keyword, count) in enumerate(analysis['top_keywords'][:5], 1):
                print(f"  {i}. {keyword} ({count}次)")
        
        # 热门标签
        if analysis['top_tags']:
            print(f"\n🏷️ 热门标签 TOP5:")
            for i, (tag, count) in enumerate(analysis['top_tags'][:5], 1):
                print(f"  {i}. {tag} ({count}次)")
        
        # 样例商品
        print(f"\n🛍️ 样例商品:")
        for i, product in enumerate(analysis['sample_products'], 1):
            print(f"  {i}. {product.get('title', 'N/A')[:50]}...")
            print(f"     价格: {product.get('price', 'N/A')}元 | 销量: {product.get('sales', 'N/A')}")
    
    # 跨场景对比
    print(f"\n" + "="*60)
    print(f"🔄 跨场景对比分析")
    print(f"="*60)
    
    # 数据完整性对比
    print(f"\n📊 各场景数据完整性对比:")
    print(f"{'场景':<15} {'价格完整率':<10} {'销量完整率':<10} {'标签完整率':<10}")
    print("-" * 50)
    
    for scenario_name, analysis in all_analysis.items():
        stats = analysis['basic_stats']
        price_rate = stats['has_price']/stats['total_products']*100
        sales_rate = stats['has_sales']/stats['total_products']*100
        tags_rate = stats['has_tags']/stats['total_products']*100
        
        print(f"{scenario_name:<15} {price_rate:<10.1f}% {sales_rate:<10.1f}% {tags_rate:<10.1f}%")
    
    # 价格对比
    print(f"\n💰 各场景价格对比:")
    print(f"{'场景':<15} {'最低价':<8} {'最高价':<8} {'平均价':<8}")
    print("-" * 45)
    
    for scenario_name, analysis in all_analysis.items():
        if analysis['price_analysis']:
            price_info = analysis['price_analysis']
            print(f"{scenario_name:<15} {price_info['min_price']:<8.2f} {price_info['max_price']:<8.2f} {price_info['avg_price']:<8.2f}")
        else:
            print(f"{scenario_name:<15} {'N/A':<8} {'N/A':<8} {'N/A':<8}")

def main():
    """主函数"""
    print("正在加载测试场景数据...")
    scenarios = load_scenario_data()
    
    if not scenarios:
        print("❌ 未找到测试场景数据文件")
        return
    
    print(f"✅ 成功加载 {len(scenarios)} 个测试场景")
    
    # 分析所有场景
    all_analysis = {}
    for scenario_name, scenario_data in scenarios.items():
        print(f"正在分析场景: {scenario_name}...")
        all_analysis[scenario_name] = analyze_scenario(scenario_name, scenario_data)
    
    # 生成报告
    generate_report(all_analysis)
    
    # 保存详细分析结果
    with open('detailed_analysis_report.json', 'w', encoding='utf-8') as f:
        json.dump(all_analysis, f, ensure_ascii=False, indent=2)
    
    print(f"\n\n📄 详细分析结果已保存到: detailed_analysis_report.json")
    print(f"🎉 分析完成！")

if __name__ == "__main__":
    main()